﻿using Abp.Domain.Repositories;
using Abp.UI;
using shenghua.Common.WordNodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.CommodityWares
{
    public class CommodityWareManager : WordNodeManager<CommodityWare>
    {
        private readonly IRepository<Commodity.Commodities.Commodity> _commodityRepository;

        public CommodityWareManager(
            IRepository<CommodityWare> commodityClassRepository,
            IRepository<Commodity.Commodities.Commodity> commodityRepository)
            : base(commodityClassRepository)
        {
            _commodityRepository = commodityRepository;
        }

        public override async Task<CommodityWare> Add(CommodityWare word)
        {
            word.Name = word.DisplayName;
            await base.Add(word);
            return word;
        }

        public override async Task<CommodityWare> Update(CommodityWare word)
        {
            word.Name = word.DisplayName;
            await base.Update(word);
            return word;
        }

        public override async Task Delete(int id)
        {
            // 检测有没有商品属于该产品库
            int count = _commodityRepository.GetAll().Where(r => r.CommodityWareId == id).Count();
            if(count > 0)
            {
                throw new UserFriendlyException($"商品库中有{count}个商品，无法删除");
            }

            await base.Delete(id);
        }
    }
}
